//
//  CellFirstFindRect.m
//  练习11
//
//  Created by 张琦 on 15/11/21.
//  Copyright © 2015年 张琦. All rights reserved.
//

#import "CellFirstFindRect.h"
#import "CollFirstFindRect.h"
#import "ModelFind.h"
#define SCR_WIDTH     [UIScreen mainScreen].bounds.size.width
#define SCR_HEIGHT    [UIScreen mainScreen].bounds.size.height

@interface CellFirstFindRect ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UILabel *labelTitle;
@property (nonatomic, retain) UIButton *buttonSectionTitle;
@property (nonatomic, retain) UICollectionViewFlowLayout *flow;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) UIView *viewSuper;
@end
@implementation CellFirstFindRect


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}


- (void)createView
{
    
    self.viewSuper = [[UIView alloc] init];
    self.viewSuper.backgroundColor = [UIColor lightGrayColor];
    [self.contentView addSubview:self.viewSuper];
    [_viewSuper release];
    
    self.labelTitle = [[UILabel alloc] init];
    self.labelTitle.backgroundColor = [UIColor cyanColor];
    [self.viewSuper addSubview:self.labelTitle];
    [_labelTitle release];
    
    self.buttonSectionTitle = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.buttonSectionTitle setTitle:@"更多" forState:UIControlStateNormal];
    self.buttonSectionTitle.backgroundColor = [UIColor cyanColor];
    [self.viewSuper addSubview:self.buttonSectionTitle];
    
    self.flow = [[UICollectionViewFlowLayout alloc] init];
    self.flow.itemSize = CGSizeMake(100, 130);
    self.flow.minimumLineSpacing = 10;
    
    self.flow.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:self.flow];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.scrollEnabled = NO;
    [self.viewSuper addSubview:self.collectionView];
    //  内存
    [_collectionView release];
    [_flow release];
    // 签协议
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    // 注册collectionView
    [self.collectionView registerClass:[CollFirstFindRect class] forCellWithReuseIdentifier:@"CollFirstFindRect"];
    
}

- (void)setArray:(NSArray *)array
{
    if (_array != array) {
        [_array release];
        _array = [array retain];
    }
    self.labelTitle.text = self.name;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CollFirstFindRect *coll = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollFirstFindRect" forIndexPath:indexPath];
    NSDictionary *dic = [self.array objectAtIndex:indexPath.row];
    coll.dic = dic;
    return coll;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [self.array objectAtIndex:indexPath.row];
    NSDictionary *dicActin = [dic objectForKey:@"action"];
    [self.delegate pushPage:[dicActin objectForKey:@"value"]];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.viewSuper.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
    self.labelTitle.frame = CGRectMake(0, 5, self.viewSuper.frame.size.width, 35);
    self.buttonSectionTitle.frame = CGRectMake(SCR_WIDTH - 70, 5, 60, 35);
    self.collectionView.frame = CGRectMake(0, 40, SCR_WIDTH, self.viewSuper.frame.size.height - 40 - 5);
}


@end
